﻿var layout = "topCenter";
var msg = null;
var mostrado = false;
var prm = null;
function pkAlert(texto) {
    var n = noty({
        text: texto,
        type: 'alert',
        dismissQueue: true,
        modal: true,
        layout: layout,
        theme: 'default'
    });
    return n;
}


function pkHecho(texto) {
    var n = noty({
        text: texto,
        type: 'success',
        dismissQueue: true,
        modal: true,
        layout: layout,
        theme: 'default'
    });
    return n;
}

function pkError(texto) {
    alert('texto');
    var n = noty({
        text: texto,
        type: 'error',
        dismissQueue: true,
        layout: layout,
        modal: true,
        theme: 'default'
    });
    return n;
}

function pkAviso(texto) {
    var n = noty({
        text: texto,
        type: 'warning',
        dismissQueue: true,
        modal: true,
        layout: layout,
        theme: 'default'
    });
    return n;
}


function pkMsgModal(texto) {
    try {

        CerrarMsgModal();

            
            msg = noty({
                text: texto,
                type: 'information',
                modal: true,
                closeWith: ['none'],
                dismissQueue: false,
                layout: layout,
                theme: 'default',
                animation: {
                    open: { height: 'toggle' },
                    close: { height: 'toggle' },
                    easing: 'fast',
                    speed: 0
                },
                buttons: false
            });
        document.body.style.cursor = "wait";
        mostrado = false;

    } catch (ex) {
        alert('error al mostrar el mensaje');
    }
}

function pkConfirm(texto) {

    var n = noty({
        text: texto,
        type: 'alert',
        dismissQueue: true,
        layout: layout,
        theme: 'default'
    });
    return n;

}

function CerrarMsgModal() {
    try {
        document.body.style.cursor = "auto";
        if (msg != null) {
            msg.close();
            msg = null;
        }
    } catch (ex) { }
}

function ConfigurarAjax() {
    try {
        prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function () {

            //CerrarMsgModal();

        });

        prm.add_beginRequest(function () {
            //pkMsgModal("Procesando solicitud, espere...");
        });

    } catch (ex) { prm = null; }
}


var CoreVentana = null;
var ventanaInfo = null;
function MostrarInfo(titulo, html, posx, posy, dock) {
    if (ventanaInfo != null) {
        ventanaInfo.close();
        ventanaInfo = null;
    }
    ventanaInfo = $.window({
        title: titulo,
        draggable: true,
        checkBoundary: true,
        showModal: false,
        minimizable: false,
        resizable:true,
        bookmarkable: false,
        scrollable: true,
        x: posx,
        dockArea: dock,
        y: posy,
        content: html,
        onClose: function () { ventanaInfo = null; }
    });
}
function MostrarVentana(titulo, url, win) {    
    CoreVentana = $.window({
        title: titulo,
        draggable: false,        
        showModal: true,
        minimizable: false,
        scrollable: true,
        resizable: true,
        bookmarkable: false,
        url: url,
        icon:null,
        width:$(window).innerWidth()-100,
        height:$(window).innerHeight()-100,
        onClose: function () { CoreVentana = null; if(win!=null)win.self.recargarThis(); }
    });


}

function cerrarCoreVentana() {
    if (CoreVentana != null) {
        CoreVentana.close();
    }
}

function sesionValida() {
    if (window.parent != null) {
        window.parent.self.cerrarCoreVentana();
        //window.parent.self.recargarThis();

    }
    else {
        cerrarCoreVentana();
        //recargarThis();        
    }
}


function recargarThis() {
    pkMsgModal('Actualizando pagina..');
    self.location.replace(self.location);

}

function login() {

    MostrarVentana('login de usuario', 'Comunes/FrmLogin.aspx', window);
}




function EjecutarAjax(url, fnExito, fnFalla) {

    $(document).ajaxStart(function () {
        //pkMsgModal('Obteniendo datos3...');
    });


    $(document).ajaxComplete(function (event, request, settings) {
        CerrarMsgModal();
    });


    if (!fnFalla)
        fnFall = function (dato, err) { alert("error" + err); };

    $.ajax({
        url: url,
        success: fnExito
    }).error(fnFalla);
}


function abrirPagina(url, msg) {
    self.location.replace(url);
    pkMsgModal(msg);
}



///COOKIES

function setCookie(c_name, value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 1);
    var c_value = escape(value) + ((1 == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

function progress(percent, $element) {    
    var progressBarWidth = percent * $element.width() / 100;
    $element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "%&nbsp;");
}